package jp.ne.hatena.d.digitalsoul.loansyndicate.models

trait SharePie {
	
	var shares:Map[Int, Share]
	
    def totalAmount:BigInt =
	    (BigInt(0) /: shares.values) ((sum, share) =>  sum + share.amount)
	
    def add(share:Share) = shares += ( share.ownerId -> share )
    
    def allot(ownerId:Int, amount:BigInt, totalAmount:BigInt):BigInt =
    	amount * shares(ownerId).amount / totalAmount
    
    def transfer(from:Company, to:Company, amount:Int) = {
		assert(BigInt(amount) <= shares(from.id).amount)
		
		shares(from.id).amount -= amount
		shares(to.id).amount += amount
	}

}